import { Injectable } from '@angular/core';

import { EventEmitter } from 'eventemitter3';

@Injectable({
    providedIn: 'root'
})
export class EventService {
    public eventEmit: any;
    constructor() {
        this.eventEmit = new EventEmitter();
    }

    emit(name, data?) {
        this.eventEmit.emit(name, data);
    }

    on(name, handler) {
        this.eventEmit.on(name, handler);
    }

    removeAllListeners(evt?) {
        this.eventEmit.removeAllListeners(evt);
    }
    removeListeners(evt, listener) {
        this.eventEmit.removeListener(evt, listener);
    }

    off(name, handler) {
        this.eventEmit.off(name, handler);
    }

    getEventCount(): number {
        return (this.eventEmit as any)._eventsCount;
    }

    removeCustomListListenersByName(name) {
        this.eventEmit.removeAllListeners(name);
    }

    removeCustomListListeners() {
        this.eventEmit.removeAllListeners("appCardListTagAskForList");
        this.eventEmit.removeAllListeners("employeListTagAskForList");
        this.eventEmit.removeAllListeners("updateCommonFilter");
        this.eventEmit.removeAllListeners("appLjGeneralListTagAskForList");
        this.eventEmit.removeAllListeners("commentsListTagAskForList");
        this.eventEmit.removeAllListeners("myApplyListTagAskForList");
        this.eventEmit.removeAllListeners("orderListTagAskForList");
        this.eventEmit.removeAllListeners("performanceInfoTagAskForList");
        this.eventEmit.removeAllListeners("performanceInfoListTagAskForList");
        this.eventEmit.removeAllListeners("qrCode");
        this.eventEmit.removeAllListeners("orderListStyle2TagAskForList");
        this.eventEmit.removeAllListeners("openPageModalWithParams");
        this.eventEmit.removeAllListeners("smoothTabChangeByIndex");
        this.eventEmit.removeAllListeners("appGeneralListTagAskForList");
        // 1.6.34 订单管理 - 全部订单更新
        this.eventEmit.removeAllListeners("orderListTimeFilterTagAskForList");
    }

    // 2023.11.24 生成UUID 例如：62689ff0-ba07-5c58-886f-1700805477080
    getUuid() {
        if (localStorage.getItem('imei')) {
            return localStorage.getItem('imei');
        }
        const s = [];
        const hexDigits = "0123456789abcdefghijklmnopqrstvuwxyz";
        for (let i = 0; i < 24; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 36), 1);
        }
        // s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        // tslint:disable-next-line:no-bitwise
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = '-';

        const uuid = s.join('');
        const imeiStr = (uuid + new Date().getTime());
        // 2023.11.30 如果imei为空则存imei
        localStorage.setItem('imei', imeiStr);
        return imeiStr;

    }

}
